package class4;

import java.util.*;

/**
 * 比较器
 */
public class Code04_ShowComparator {
    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 5, 6, 8, 9, 6, 4, 2, 4, 3, 9, 10, 7, 22};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));

        Student s1 = new Student(3, "张三", 23);
        Student s2 = new Student(1, "李四", 23);
        Student s3 = new Student(4, "王五", 23);
        Student s4 = new Student(2, "赵六", 23);
        Student s5 = new Student(6, "左七", 23);
        Student s6 = new Student(5, "刘能", 23);

        Student[] students = {s1, s2, s3, s4, s5, s6};
        Arrays.sort(students, new IdComparator());
        printStudent(students);
        List<Student> list = new ArrayList<>();
        list.sort(new IdComparator());

    }

    private static void printStudent(Student[] students) {
        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i]);
        }
    }

    /**
     * 比较器
     */
    public static class IdComparator implements Comparator<Student> {
        @Override
        public int compare(Student o1, Student o2) {
            return o1.id < o2.id ? -1 : o1.id == o2.id ? 0 : 1;
        }
    }

    static class Student {

        public int id;
        public String name;
        public int age;

        public Student(int id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        public Student() {

        }

        @Override
        public String toString() {
            return "Student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }

    }
}
